package cn.zhqr.common.util.str;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by yantingjun on 2018/11/22.
 */
public class StringUtils {
    private static final String SEPARATOR_CHAR = ",";

    /**
     * 从左侧填充字符至指定长度
     * @param str         输入字符串
     * @param length      指定的字符串长度
     * @param ch          填充字符
     * @return
     */
    public static String leftPad(String str,int length,char ch){
        return padding(str,length,ch,length-str.length());
    }

    /**
     * 从右侧填充字符至指定长度
     * @param str         输入字符串
     * @param length      指定的字符串长度
     * @param ch          填充字符
     * @return
     */
    public static String rightPad(String str,int length,char ch){
        return padding(str,length,ch,0);
    }

    /**
     * 填充字符至指定长度
     * @param str              输入字符串
     * @param length           指定的字符串长度
     * @param ch               填充字符
     * @param startPosition    从指定的位置开始填充
     * @return
     */
    private static String padding(String str,int length,char ch,int startPosition){
        if(str==null){
            str = "";
        }
        if(str.length()>=length){
            return str;
        }
        char[] chs = new char[length];
        Arrays.fill(chs, ch);
        char[] src = str.toCharArray();
        System.arraycopy(src, 0, chs, startPosition,src.length);
        return new String(chs);
    }

    public static String removeBlank(String str){
        if(str!=null){
            str = str.replaceAll("\\p{C}", "").replace(" ","");
        }
        return str;
    }

    public static String null2Str(Object value){
        if(value==null){
            return "";
        }
        return String.valueOf(value);
    }

    //去除emoji表情和空格
    public static String filterSupplement(String content) {
        content = trim(content);
        StringBuilder sb = new StringBuilder();
        for (char ch : content.toCharArray()) {
            if (!Character.isHighSurrogate(ch) && !Character.isLowSurrogate(ch)) {
                sb.append(ch);
            }
        }
        return sb.toString();
    }

    public static String filterEmoji(String source,String replacement) {
        if (isBlank(source)) {
            return source;
        }
        if(replacement == null){
            replacement = "";
        }
        Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]||[\ud83e\udc00-\ud83e\udfff]|[\u2600-\u27ff]",
                Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher emojiMatcher = emoji.matcher(source);
        if (emojiMatcher.find()) {
            source = emojiMatcher.replaceAll(replacement);
            return source;
        }
        return source;
    }

    public static String trim(String str) {
        return str == null ? null : str.trim();
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(cs);
    }

    public static boolean isBlank(CharSequence cs) {
        int strLen = length(cs);
        if (strLen == 0) {
            return true;
        } else {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        }
    }

    public static boolean isNotBlank(CharSequence cs) {
        return !isBlank(cs);
    }

    public static int length(CharSequence cs) {
        return cs == null ? 0 : cs.length();
    }





    public static String join(boolean[] array, char delimiter) {
        return array == null ? null : join((boolean[])array, delimiter, 0, array.length);
    }

    public static String join(byte[] array, char delimiter) {
        return array == null ? null : join((byte[])array, delimiter, 0, array.length);
    }

    public static String join(char[] array, char delimiter) {
        return array == null ? null : join((char[])array, delimiter, 0, array.length);
    }

    public static String join(double[] array, char delimiter) {
        return array == null ? null : join((double[])array, delimiter, 0, array.length);
    }

    public static String join(float[] array, char delimiter) {
        return array == null ? null : join((float[])array, delimiter, 0, array.length);
    }

    public static String join(int[] array, char separator) {
        return array == null ? null : join((int[])array, separator, 0, array.length);
    }

    public static String join(Iterable<?> iterable, char separator) {
        return iterable == null ? null : join(iterable.iterator(), separator);
    }

    public static String join(Iterable<?> iterable, String separator) {
        return iterable == null ? null : join(iterable.iterator(), separator);
    }

    public static String join(Iterator<?> iterator, char separator) {
        if (iterator == null) {
            return null;
        } else if (!iterator.hasNext()) {
            return "";
        } else {
            Object first = iterator.next();
            if (!iterator.hasNext()) {
                return toStringOrEmpty(first);
            } else {
                StringBuilder buf = new StringBuilder(256);
                if (first != null) {
                    buf.append(first);
                }

                while(iterator.hasNext()) {
                    buf.append(separator);
                    Object obj = iterator.next();
                    if (obj != null) {
                        buf.append(obj);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static String join(Iterator<?> iterator, String separator) {
        if (iterator == null) {
            return null;
        } else if (!iterator.hasNext()) {
            return "";
        } else {
            Object first = iterator.next();
            if (!iterator.hasNext()) {
                return Objects.toString(first, "");
            } else {
                StringBuilder buf = new StringBuilder(256);
                if (first != null) {
                    buf.append(first);
                }

                while(iterator.hasNext()) {
                    if (separator != null) {
                        buf.append(separator);
                    }

                    Object obj = iterator.next();
                    if (obj != null) {
                        buf.append(obj);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static String join(List<?> list, char separator, int startIndex, int endIndex) {
        if (list == null) {
            return null;
        } else {
            int noOfItems = endIndex - startIndex;
            if (noOfItems <= 0) {
                return "";
            } else {
                List<?> subList = list.subList(startIndex, endIndex);
                return join(subList.iterator(), separator);
            }
        }
    }

    public static String join(List<?> list, String separator, int startIndex, int endIndex) {
        if (list == null) {
            return null;
        } else {
            int noOfItems = endIndex - startIndex;
            if (noOfItems <= 0) {
                return "";
            } else {
                List<?> subList = list.subList(startIndex, endIndex);
                return join(subList.iterator(), separator);
            }
        }
    }

    public static String join(long[] array, char separator) {
        return array == null ? null : join((long[])array, separator, 0, array.length);
    }

    public static String join(Object[] array, char delimiter) {
        return array == null ? null : join((Object[])array, delimiter, 0, array.length);
    }

    public static String join(Object[] array, String delimiter) {
        return array == null ? null : join((Object[])array, delimiter, 0, array.length);
    }


    public static String join(short[] array, char delimiter) {
        return array == null ? null : join((short[])array, delimiter, 0, array.length);
    }


    @SafeVarargs
    public static <T> String join(T... elements) {
        return join((Object[])elements, (String)null);
    }

    private static String toStringOrEmpty(Object obj) {
        return Objects.toString(obj, "");
    }

    public static List<String> split(String str, String separatorChar) {
        return split(str, separatorChar, false);
    }

    public static List<String> split(String str, String separatorChar, boolean distinct) {
        if (StringUtils.isBlank(str)) {
            return new ArrayList();
        } else {
            List<String> stringList = new ArrayList();
            String[] arr = splitWorker(str, separatorChar,-1,false);

            for(int i = 0; i < arr.length; ++i) {
                String s = arr[i];
                if (!distinct || !stringList.contains(s)) {
                    stringList.add(s);
                }
            }

            return stringList;
        }
    }

    private static String[]



    splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) {
        if (str == null) {
            return null;
        } else {
            int len = str.length();
            if (len == 0) {
                return new String[0];
            } else {
                List list = new ArrayList();
                int sizePlus1 = 1;
                int i = 0;
                int start = 0;
                boolean match = false;
                boolean lastMatch = false;
                if (separatorChars != null) {
                    if (separatorChars.length() != 1) {
                        label87:
                        while(true) {
                            while(true) {
                                if (i >= len) {
                                    break label87;
                                }

                                if (separatorChars.indexOf(str.charAt(i)) >= 0) {
                                    if (match || preserveAllTokens) {
                                        lastMatch = true;
                                        if (sizePlus1++ == max) {
                                            i = len;
                                            lastMatch = false;
                                        }

                                        list.add(str.substring(start, i));
                                        match = false;
                                    }

                                    ++i;
                                    start = i;
                                } else {
                                    lastMatch = false;
                                    match = true;
                                    ++i;
                                }
                            }
                        }
                    } else {
                        char sep = separatorChars.charAt(0);

                        label71:
                        while(true) {
                            while(true) {
                                if (i >= len) {
                                    break label71;
                                }

                                if (str.charAt(i) == sep) {
                                    if (match || preserveAllTokens) {
                                        lastMatch = true;
                                        if (sizePlus1++ == max) {
                                            i = len;
                                            lastMatch = false;
                                        }

                                        list.add(str.substring(start, i));
                                        match = false;
                                    }

                                    ++i;
                                    start = i;
                                } else {
                                    lastMatch = false;
                                    match = true;
                                    ++i;
                                }
                            }
                        }
                    }
                } else {
                    label103:
                    while(true) {
                        while(true) {
                            if (i >= len) {
                                break label103;
                            }

                            if (Character.isWhitespace(str.charAt(i))) {
                                if (match || preserveAllTokens) {
                                    lastMatch = true;
                                    if (sizePlus1++ == max) {
                                        i = len;
                                        lastMatch = false;
                                    }

                                    list.add(str.substring(start, i));
                                    match = false;
                                }

                                ++i;
                                start = i;
                            } else {
                                lastMatch = false;
                                match = true;
                                ++i;
                            }
                        }
                    }
                }

                if (match || preserveAllTokens && lastMatch) {
                    list.add(str.substring(start, i));
                }

                return (String[])((String[])list.toArray(new String[list.size()]));
            }
        }
    }

    public static List<String> split(String str) {
        return split(str, ",");
    }

    public static List<Integer> split2Int(String str, String separatorChar, boolean distinct) {
        if (StringUtils.isBlank(str)) {
            return new ArrayList();
        } else {
            List<Integer> intList = new ArrayList();
            String[] arr = splitWorker(str, separatorChar,-1,false);

            for(int i = 0; i < arr.length; ++i) {
                String s = arr[i];
                if (!StringUtils.isBlank(s)) {
                    try {
                        Integer integer = Integer.parseInt(s.trim());
                        if (!distinct || !intList.contains(integer)) {
                            intList.add(integer);
                        }
                    } catch (Exception var9) {
                        System.out.println("Failed to parse string '" + s + "' to int!");
                    }
                }
            }

            return intList;
        }
    }

    public static List<Integer> split2Int(String str, String separator_char) {
        return split2Int(str, separator_char, true);
    }

    public static List<Integer> split2Int(String str) {
        return split2Int(str, ",");
    }

    public static List<Long> split2Long(String str, String separatorChar, boolean distinct) {
        if (StringUtils.isBlank(str)) {
            return new ArrayList();
        } else {
            List<Long> intList = new ArrayList();
            String[] arr = splitWorker(str, separatorChar,-1,false);

            for(int i = 0; i < arr.length; ++i) {
                String s = arr[i];
                if (!StringUtils.isBlank(s)) {
                    try {
                        Long lng = Long.parseLong(s.trim());
                        if (!distinct || !intList.contains(lng)) {
                            intList.add(lng);
                        }
                    } catch (Exception var9) {
                        System.out.println("Failed to parse string '" + s + "' to Long!");
                    }
                }
            }

            return intList;
        }
    }

    public static List<Long> split2Long(String str, String separator_char) {
        return split2Long(str, separator_char, true);
    }

    public static List<Long> split2Long(String str) {
        return split2Long(str, ",");
    }
}
